using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace SevenWizards1.CoreComponents
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class TileMap : Microsoft.Xna.Framework.DrawableGameComponent
    {
        List<TileMapLayer> tileMapLayers = new List<TileMapLayer>();

        Random random = new Random();
        SpriteBatch spriteBatch;

        public TileMap(int tileMapWidth, int tileMapHeight, Game game)
            : base(game)
        {
            spriteBatch =
                (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            TileMapLayer layer = new TileMapLayer(tileMapWidth, tileMapHeight);
            
            for (int x = 0; x < 50; x++)
                for (int y = 0; y < 50; y++)
                    layer.SetTile(x, y, random.Next(0, 4));
            tileMapLayers.Add(layer);
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            foreach (TileMapLayer layer in tileMapLayers)
            {
                for (int x = 0; x < TileEngine.TileMapWidth; x++)
                {
                    for (int y = 0; y < TileEngine.TileMapHeight; y++)
                    {
                        spriteBatch.Draw(TileEngine.TileSet,
                            new Rectangle(x * TileEngine.TileWidth -
                                    TileEngine.CameraXPosition,
                                y * TileEngine.TileHeight -
                                    TileEngine.CameraYPositon,
                                TileEngine.TileWidth,
                                TileEngine.TileHeight),
                            TileEngine.Tiles[layer.GetTile(x, y)],
                            Color.White);
                    }
                }
            }

            base.Draw(gameTime);
        }

        public virtual void Hide()
        {
            Visible = false;
            Enabled = false;
        }

        public virtual void Show()
        {
            Visible = true;
            Enabled = true;
        }
    }
}